import Vue from 'vue'
import VueRouter from 'vue-router'

import i18n from '../main.js'

import Wrapper from '@/views/Wrapper.vue'
import Login from '@/views/Login.vue'
/**
 * 重写路由的push replace方法
 * 解决，相同路由跳转时，报错
 */
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch((err) => err)
}
VueRouter.prototype.replace = function replace (location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalReplace.call(this, location, onResolve, onReject)
  }
  return originalReplace.call(this, location).catch((err) => err)
}

Vue.use(VueRouter)

export const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },
  {
    path: '/',
    redirect: '/newDevices', //重定向
    name: 'Home',
    component: Wrapper,
    children: [
      {
        path: 'device',
        redirect: '/device/list',
        name: 'Device',
        meta: { title: '设备列表' },
        component: () => import('@/views/devices/index.vue'),
        children: [
          {
            path: 'list',
            name: 'DeviceList',
            meta: { title: '设备列表' },
            component: () => import('@/views/devices/List/index.vue'),
          },
        ],
      },
      {
        path: 'visitors',
        redirect: '/visitors/list',
        name: 'Visitors',
        meta: { title: '访客管理' },
        component: () => import('@/views/visitors/index.vue'),
        children: [
          {
            path: 'visitorsList',
            name: 'VisitorsList',
            meta: { title: '访客管理' },
            component: () => import('@/views/visitors/List/index.vue'),
          },
        ],
      },
      {
        path: 'version',
        name: 'Version',
        meta: { title: '终端版本管理' },
        component: () => import('@/views/Version/index.vue'),
      },
      {
        path: 'staff',
        name: 'Staff',
        meta: { title: '员工管理' },
        component: () => import('@/views/Staff/index.vue'),
      },
      {
        path: 'permissions',
        name: 'Permissions',
        meta: { title: '权限管理' },
        component: () => import('@/views/permissions/index.vue'),
      },
      {
        path: 'area',
        name: 'Area',
        meta: { title: '区域管理' },
        component: () => import('@/views/Area/index.vue'),
      },
      {
        path: 'attendance',
        name: 'Attendance',
        meta: { title: '考勤管理' },
        component: () => import('@/views/Attendance/index.vue'),
      },
      {
        path: 'body-temperature',
        name: 'BodyTemperature',
        meta: { title: '测温记录' },
        component: () => import('@/views/Record/BodyTemperature/index.vue'),
      },
      {
        path: 'access-record',
        name: 'AccessRecord',
        meta: { title: '访问记录' },
        component: () => import('@/views/Record/Access/index.vue'),
      },
      {
        path: 'uLog',
        name: 'ULog',
        meta: { title: '操作日志' },
        component: () => import('@/views/ULog/index.vue'),
      },
      {
        path: 'table',
        name: 'Table',
        meta: { title: '图表测试' },
        component: () => import('@/views/Table/index.vue'),
      },
      {
        path: 'emailAdmin',
        name: 'EmailAdmin',
        meta: { title: '邮箱管理' },
        component: () => import('@/views/Email/index.vue'),
      },
      {
        path: 'companyManagement',
        name: 'companyManagement',
        meta: { title: '公司管理' },
        component: () => import('@/views/companyManagement/index.vue'),
      },
      {
        path: 'warningMessage',
        name: 'WarningMessage',
        meta: { title: '报警信息' },
        component: () => import('@/views/warningMessage/index.vue'),
      },
      {
        path: 'newDevices',
        name: 'NewDevices',
        meta: { title: '场所管理' },
        component: () => import('@/views/newDevices/index.vue'),
      },
      {
        path: 'advertising',
        name: 'Advertising',
        meta: { title: '广告管理' },
        component: () => import('@/views/advertising/index.vue'),
      },
    ],
  },
]

const router = new VueRouter({
  routes,
  scrollBehavior (to, from, savedPosition) {
    // console.log('scrollBehavior',to,from,'savedPosition',savedPosition)
    if (savedPosition) {
      return savedPosition
    }
    return { x: 0, y: 0 }
    // 异步滚动操作
    // return new Promise((resolve) => {
    //   setTimeout(() => {
    //   resolve({ x: 0, y: 1 });
    //   }, 0);
    // });
  },
})

router.beforeEach((to, from, next) => {
  // waltz.navigator.hide()
  // const { meta } = to
  // if (meta && meta.title) {
  //   document.title = meta.title
  // }
  // console.log('Vue.i18n',to.name)
  if(to.name==='DeviceList'){
    document.title = i18n.t('navigationLeft.equipment')
  }else if(to.name==='Staff'){
    document.title = i18n.t('navigationLeft.staff')
  }else if(to.name==='Attendance'){
    document.title = i18n.t('Attendance.AttendanceManage')
  }else if(to.name==='VisitorsList'){
    document.title = i18n.t('navigationLeft.visitors')
  }else if(to.name==='BodyTemperature'){
    document.title = i18n.t('navigationLeft.bodyTemperature')
  }else if(to.name==='AccessRecord'){
    document.title = i18n.t('navigationLeft.accessRecord')
  }else if(to.name==='ULog'){
    document.title = i18n.t('UlogI18n.UlogI18nTit')
  }else if(to.name==='Permissions'){
    document.title = i18n.t('navigationLeft.permissions')
  }else if(to.name==='EmailAdmin'){
    document.title = i18n.t('navigationLeft.emailAdmin')
  }else if(to.name==='WarningMessage'){
    document.title = i18n.t('navigationLeft.warningMessage')
  }else if(to.name==='NewDevices'){
    document.title = i18n.t('navigationLeft.newDevices')
  }else if(to.name==='Version'){
    document.title = i18n.t('navigationLeft.TerminalVersion')
  }

  next()
})

router.onError((error) => {
  console.error(error)
  console.log('路由报错啦')
})

export default router
